#include <iostream>
#include "Mux.h"

int Write_Pat(unsigned char * buf, IMuxOutput *pOutput)
{
	WriteStruct_Pat(buf, pOutput->GetMuxItem());
	pOutput->OnTsPacket(buf,TS_PACKET_SIZE, 0);
//	return WriteFile(pVideo_Audio_Ts_File,(char *)buf,TS_PACKET_SIZE);
	return 1;
}

int Write_Pmt(unsigned char * buf, IMuxOutput *pOutput)
{
	WriteStruct_Pmt(buf, pOutput->GetMuxItem());
	pOutput->OnTsPacket(buf, TS_PACKET_SIZE, 0);
//	return WriteFile(pVideo_Audio_Ts_File,(char *)buf,TS_PACKET_SIZE);
	return 1;
}

int SendPushData(unsigned char *lpszBuf, int nLen, long long lTimestamp, IMuxOutput *pOutput)
{
//	int result = fwrite(lpszBuf,TS_PACKET_SIZE,1,pVideo_Audio_Ts_File);

	return pOutput->OnTsPacket(lpszBuf, TS_PACKET_SIZE, 0);
}

int Take_Out_Pes(TsPes * tspes, unsigned long time_pts, unsigned int frametype, unsigned int *videoframetype, unsigned char *pData, int iDataSize, unsigned char uVideoEncType = 0)
{
	unsigned int pes_pos = 0;
	if (frametype == 0x00) //视频
	{
		pes_pos = VES2PES(tspes, time_pts, videoframetype,pData,iDataSize, uVideoEncType);
	}
	else                   //音频
	{
		pes_pos = AAC2PES(tspes, time_pts,pData,iDataSize);
	}
	return pes_pos;
}

int WriteAdaptive_flags_Head(Ts_Adaptation_field  * ts_adaptation_field,unsigned int Videopts)
{
	//填写自适应段
	ts_adaptation_field->discontinuty_indicator = 0;
	ts_adaptation_field->random_access_indicator = 0;
	ts_adaptation_field->elementary_stream_priority_indicator = 0;
	ts_adaptation_field->PCR_flag = 1;                                          //只用到这个
	ts_adaptation_field->OPCR_flag = 0;
	ts_adaptation_field->splicing_point_flag = 0;
	ts_adaptation_field->transport_private_data_flag = 0;
	ts_adaptation_field->adaptation_field_extension_flag = 0;

	//需要自己算
	ts_adaptation_field->pcr  = Videopts * 300ULL;
    //ts_adaptation_field->pcr = Videopts;
	ts_adaptation_field->adaptation_field_length = 7;                          //占用7位

	ts_adaptation_field->opcr = 0;
	ts_adaptation_field->splice_countdown = 0;
	ts_adaptation_field->private_data_len = 0;
	return 1;
}

int WriteAdaptive_flags_Tail(Ts_Adaptation_field  * ts_adaptation_field)
{
	//填写自适应段
	ts_adaptation_field->discontinuty_indicator = 0;
	ts_adaptation_field->random_access_indicator = 0;
	ts_adaptation_field->elementary_stream_priority_indicator = 0;
	ts_adaptation_field->PCR_flag = 0;                                         //只用到这个
	ts_adaptation_field->OPCR_flag = 0;
	ts_adaptation_field->splicing_point_flag = 0;
	ts_adaptation_field->transport_private_data_flag = 0;
	ts_adaptation_field->adaptation_field_extension_flag = 0;

	//需要自己算
	ts_adaptation_field->pcr  = 0;
	ts_adaptation_field->adaptation_field_length = 1;                          //占用1位标志所用的位

	ts_adaptation_field->opcr = 0;
	ts_adaptation_field->splice_countdown = 0;
	ts_adaptation_field->private_data_len = 0;                    
	return 1;
}

int CreateAdaptive_Ts(Ts_Adaptation_field * ts_adaptation_field,unsigned char * buf,unsigned int AdaptiveLength)
{
	unsigned int CurrentAdaptiveLength = 1;                                 //当前已经用的自适应段长度
	unsigned char Adaptiveflags = 0;                                        //自适应段的标志
	unsigned int adaptive_pos = 0;

	//填写自适应字段
	if (ts_adaptation_field->adaptation_field_length > 0)
	{
		adaptive_pos += 1;                                                  //自适应段的一些标志所占用的1个字节
		CurrentAdaptiveLength += 1;

		if (ts_adaptation_field->discontinuty_indicator)
		{
			Adaptiveflags |= 0x80;
		}
		if (ts_adaptation_field->random_access_indicator)
		{
			Adaptiveflags |= 0x40;
		}
		if (ts_adaptation_field->elementary_stream_priority_indicator)
		{
			Adaptiveflags |= 0x20;
		}
		if (ts_adaptation_field->PCR_flag)
		{
			unsigned long long pcr_base;
			unsigned int pcr_ext;

			pcr_base = (ts_adaptation_field->pcr / 300);
			pcr_ext = (ts_adaptation_field->pcr % 300);

            //pcr_base = ts_adaptation_field->pcr;
            //pcr_ext = 0;

			Adaptiveflags |= 0x10;

			buf[adaptive_pos + 0] = (pcr_base >> 25) & 0xff;
			buf[adaptive_pos + 1] = (pcr_base >> 17) & 0xff;
			buf[adaptive_pos + 2] = (pcr_base >> 9) & 0xff;
			buf[adaptive_pos + 3] = (pcr_base >> 1) & 0xff;
			buf[adaptive_pos + 4] = pcr_base << 7 | pcr_ext >> 8 | 0x7e;
			buf[adaptive_pos + 5] = (pcr_ext) & 0xff;
			adaptive_pos += 6;

			CurrentAdaptiveLength += 6;
		}
		if (ts_adaptation_field->OPCR_flag)
		{
			unsigned long long opcr_base;
			unsigned int opcr_ext;

			opcr_base = (ts_adaptation_field->opcr / 300);
			opcr_ext = (ts_adaptation_field->opcr % 300);

			Adaptiveflags |= 0x08;

			buf[adaptive_pos + 0] = (opcr_base >> 25) & 0xff;
			buf[adaptive_pos + 1] = (opcr_base >> 17) & 0xff;
			buf[adaptive_pos + 2] = (opcr_base >> 9) & 0xff;
			buf[adaptive_pos + 3] = (opcr_base >> 1) & 0xff;
			buf[adaptive_pos + 4] = ((opcr_base << 7) & 0x80) | ((opcr_ext >> 8) & 0x01);
			buf[adaptive_pos + 5] = (opcr_ext) & 0xff;
			adaptive_pos += 6;
			CurrentAdaptiveLength += 6;
		}
		if (ts_adaptation_field->splicing_point_flag)
		{
			buf[adaptive_pos] = ts_adaptation_field->splice_countdown;

			Adaptiveflags |= 0x04;

			adaptive_pos += 1;
			CurrentAdaptiveLength += 1;
		}
		if (ts_adaptation_field->private_data_len > 0)
		{
			Adaptiveflags |= 0x02;
			if (1+ ts_adaptation_field->private_data_len > (int)(AdaptiveLength - CurrentAdaptiveLength))
			{
				return getchar();
			}
			else
			{
				buf[adaptive_pos] = ts_adaptation_field->private_data_len;
				adaptive_pos += 1;
				memcpy (buf + adaptive_pos, ts_adaptation_field->private_data, ts_adaptation_field->private_data_len);
				adaptive_pos += ts_adaptation_field->private_data_len;

				CurrentAdaptiveLength += (1 + ts_adaptation_field->private_data_len) ;
			}
		}
		if (ts_adaptation_field->adaptation_field_extension_flag)
		{
			Adaptiveflags |= 0x01;
			buf[adaptive_pos + 1] = 1;
			buf[adaptive_pos + 2] = 0;
			CurrentAdaptiveLength += 2;
		}
		buf[0] = Adaptiveflags;                        //将标志放入内存
	}
	return 1;
}

int PES2TS(TsPes * ts_pes,unsigned int Video_Audio_PID ,Ts_Adaptation_field * ts_adaptation_field_Head ,Ts_Adaptation_field * ts_adaptation_field_Tail,
		   unsigned long  Videopts,unsigned long Adudiopts, IMuxOutput *pOutput)
{
    MuxItem *pMuxItem = pOutput->GetMuxItem();

	if (pMuxItem->isInsertPatPmt == 0)
    {
		Write_Pat(pMuxItem->m_One_Frame_Buf, pOutput);
		Write_Pmt(pMuxItem->m_One_Frame_Buf, pOutput);
        pMuxItem->isInsertPatPmt = 1;
	}


	unsigned int ts_pos = 0;
	unsigned int FirstPacketLoadLength = 0 ;                                   //分片包的第一个包的负载长度
	unsigned int NeafPacketCount = 0;                                          //分片包的个数
	//unsigned int AdaptiveLength = 0;                                           //要填写0XFF的长度
	unsigned char * NeafBuf = NULL;                                            //分片包 总负载的指针
	unsigned char TSbuf[TS_PACKET_SIZE];

	memset(TSbuf,0,TS_PACKET_SIZE); 
	FirstPacketLoadLength = 188 - 4 - 1 - ts_adaptation_field_Head->adaptation_field_length - 14; //计算分片包的第一个包的负载长度
	NeafPacketCount += 1;                                                                   //第一个分片包

	//一个包的情况
	if (ts_pes->Pes_Packet_Length_Beyond < FirstPacketLoadLength)                           //这里是 sps ，pps ，sei等
	{
		memset(TSbuf,0xFF,TS_PACKET_SIZE);
		WriteStruct_Packetheader(TSbuf,Video_Audio_PID,0x01,0x03, pMuxItem);   //PID = TS_H264_PID,有效荷载单元起始指示符_play_init = 0x01, ada_field_C,0x03,含有调整字段和有效负载
		ts_pos += 4;
		TSbuf[ts_pos + 0] = 184 - ts_pes->Pes_Packet_Length_Beyond - 9 - 5 - 1 ;
		TSbuf[ts_pos + 1] = 0x00;
		ts_pos += 2; 
		memset(TSbuf + ts_pos,0xFF,(184 - ts_pes->Pes_Packet_Length_Beyond - 9 - 5 - 2));
		ts_pos += (184 - ts_pes->Pes_Packet_Length_Beyond - 9 - 5 - 2);

		TSbuf[ts_pos + 0] = (ts_pes->packet_start_code_prefix >> 16) & 0xFF;
		TSbuf[ts_pos + 1] = (ts_pes->packet_start_code_prefix >> 8) & 0xFF; 
		TSbuf[ts_pos + 2] = ts_pes->packet_start_code_prefix & 0xFF;
		TSbuf[ts_pos + 3] = ts_pes->stream_id;
		TSbuf[ts_pos + 4] = ((ts_pes->PES_packet_length) >> 8) & 0xFF;
		TSbuf[ts_pos + 5] = (ts_pes->PES_packet_length) & 0xFF;
		TSbuf[ts_pos + 6] = ts_pes->marker_bit << 6 | ts_pes->PES_scrambling_control << 4 | ts_pes->PES_priority << 3 |
			ts_pes->data_alignment_indicator << 2 | ts_pes->copyright << 1 |ts_pes->original_or_copy;
		TSbuf[ts_pos + 7] = ts_pes->PTS_DTS_flags << 6 |ts_pes->ESCR_flag << 5 | ts_pes->ES_rate_flag << 4 |
			ts_pes->DSM_trick_mode_flag << 3 | ts_pes->additional_copy_info_flag << 2 | ts_pes->PES_CRC_flag << 1 | ts_pes->PES_extension_flag;
		TSbuf[ts_pos + 8] = ts_pes->PES_header_data_length;
		ts_pos += 9;

		if (ts_pes->stream_id == TS_H264_STREAM_ID || ts_pes->stream_id == TS_H265_STREAM_ID)
		{
			TSbuf[ts_pos + 0] = (((0x3 << 4) | ((Videopts>> 29) & 0x0E) | 0x01) & 0xff);
			TSbuf[ts_pos + 1]= (((((Videopts >> 14) & 0xfffe) | 0x01) >> 8) & 0xff);
			TSbuf[ts_pos + 2]= ((((Videopts >> 14) & 0xfffe) | 0x01) & 0xff);
			TSbuf[ts_pos + 3]= (((((Videopts << 1) & 0xfffe) | 0x01) >> 8) & 0xff);
			TSbuf[ts_pos + 4]= ((((Videopts << 1) & 0xfffe) | 0x01) & 0xff);
			ts_pos += 5;

		}
		else if (ts_pes->stream_id == TS_AAC_STREAM_ID)
		{
			TSbuf[ts_pos + 0] = (((0x3 << 4) | ((Adudiopts>> 29) & 0x0E) | 0x01) & 0xff);
			TSbuf[ts_pos + 1]= (((((Adudiopts >> 14) & 0xfffe) | 0x01) >> 8) & 0xff);
			TSbuf[ts_pos + 2]= ((((Adudiopts >> 14) & 0xfffe) | 0x01) & 0xff);
			TSbuf[ts_pos + 3]= (((((Adudiopts << 1) & 0xfffe) | 0x01) >> 8) & 0xff);
			TSbuf[ts_pos + 4]= ((((Adudiopts << 1) & 0xfffe) | 0x01) & 0xff);
			ts_pos += 5;
		}
		else
		{
			printf("ts_pes->stream_id  error 0x%x \n",ts_pes->stream_id);
			return getchar();
		}
		memcpy(TSbuf + ts_pos,ts_pes->Es,ts_pes->Pes_Packet_Length_Beyond);
		pOutput->OnTsPacket(TSbuf, TS_PACKET_SIZE, 0);

        pMuxItem->WritePacketNum ++;                                                       //已经写入文件的包个数++
		return pMuxItem->WritePacketNum;
	}

	
	NeafPacketCount += (ts_pes->Pes_Packet_Length_Beyond - FirstPacketLoadLength)/ 184;     
	NeafPacketCount += 1;                                                                   //最后一个分片包
//	AdaptiveLength = 188 - 4 - 1 - ((ts_pes->Pes_Packet_Length_Beyond - FirstPacketLoadLength)% 184)  ;  //要填写0XFF的长度
//	if ((WritePacketNum % 40) == 0)                                                         //每40个包打一个 pat,一个pmt
//	{
//		Write_Pat(m_One_Frame_Buf);                                                         //创建PAT
//		Write_Pmt(m_One_Frame_Buf);                                                         //创建PMT
//	}
	//开始处理第一个包,分片包的个数最少也会是两个
	WriteStruct_Packetheader(TSbuf,Video_Audio_PID,0x01,0x03, pMuxItem);                              //PID = TS_H264_PID,有效荷载单元起始指示符_play_init = 0x01, ada_field_C,0x03,含有调整字段和有效负载 ；
	ts_pos += 4;
	TSbuf[ts_pos] = ts_adaptation_field_Head->adaptation_field_length;                      //自适应字段的长度，自己填写的
	ts_pos += 1;                                                       

	CreateAdaptive_Ts(ts_adaptation_field_Head,TSbuf + ts_pos,(188 - 4 - 1 - 14));          //填写自适应字段
	ts_pos += ts_adaptation_field_Head->adaptation_field_length;                            //填写自适应段所需要的长度

	TSbuf[ts_pos + 0] = (ts_pes->packet_start_code_prefix >> 16) & 0xFF;
	TSbuf[ts_pos + 1] = (ts_pes->packet_start_code_prefix >> 8) & 0xFF; 
	TSbuf[ts_pos + 2] = ts_pes->packet_start_code_prefix & 0xFF;
	TSbuf[ts_pos + 3] = ts_pes->stream_id;
	TSbuf[ts_pos + 4] = ((ts_pes->PES_packet_length) >> 8) & 0xFF;
	TSbuf[ts_pos + 5] = (ts_pes->PES_packet_length) & 0xFF;
	TSbuf[ts_pos + 6] = ts_pes->marker_bit << 6 | ts_pes->PES_scrambling_control << 4 | ts_pes->PES_priority << 3 |
		ts_pes->data_alignment_indicator << 2 | ts_pes->copyright << 1 |ts_pes->original_or_copy;
	TSbuf[ts_pos + 7] = ts_pes->PTS_DTS_flags << 6 |ts_pes->ESCR_flag << 5 | ts_pes->ES_rate_flag << 4 |
		ts_pes->DSM_trick_mode_flag << 3 | ts_pes->additional_copy_info_flag << 2 | ts_pes->PES_CRC_flag << 1 | ts_pes->PES_extension_flag;
	TSbuf[ts_pos + 8] = ts_pes->PES_header_data_length;
	ts_pos += 9;

	if (ts_pes->stream_id == TS_H264_STREAM_ID || ts_pes->stream_id == TS_H265_STREAM_ID)
	{
		TSbuf[ts_pos + 0] = (((0x3 << 4) | ((Videopts>> 29) & 0x0E) | 0x01) & 0xff);
		TSbuf[ts_pos + 1]= (((((Videopts >> 14) & 0xfffe) | 0x01) >> 8) & 0xff);
		TSbuf[ts_pos + 2]= ((((Videopts >> 14) & 0xfffe) | 0x01) & 0xff);
		TSbuf[ts_pos + 3]= (((((Videopts << 1) & 0xfffe) | 0x01) >> 8) & 0xff);
		TSbuf[ts_pos + 4]= ((((Videopts << 1) & 0xfffe) | 0x01) & 0xff);
		ts_pos += 5;

	}
	else if (ts_pes->stream_id == TS_AAC_STREAM_ID)
	{
		TSbuf[ts_pos + 0] = (((0x3 << 4) | ((Adudiopts>> 29) & 0x0E) | 0x01) & 0xff);
		TSbuf[ts_pos + 1]= (((((Adudiopts >> 14) & 0xfffe) | 0x01) >> 8) & 0xff);
		TSbuf[ts_pos + 2]= ((((Adudiopts >> 14) & 0xfffe) | 0x01) & 0xff);
		TSbuf[ts_pos + 3]= (((((Adudiopts << 1) & 0xfffe) | 0x01) >> 8) & 0xff);
		TSbuf[ts_pos + 4]= ((((Adudiopts << 1) & 0xfffe) | 0x01) & 0xff);
		ts_pos += 5;
	}
	else
	{
		printf("ts_pes->stream_id  error 0x%x \n",ts_pes->stream_id);
		return getchar();
	}

	NeafBuf = ts_pes->Es ;
	memcpy(TSbuf + ts_pos,NeafBuf,FirstPacketLoadLength);  

	NeafBuf += FirstPacketLoadLength;
	ts_pes->Pes_Packet_Length_Beyond -= FirstPacketLoadLength;
	pOutput->OnTsPacket(TSbuf, TS_PACKET_SIZE, 0);
	//将包写入文件
//	fwrite(TSbuf,188,1,pVideo_Audio_Ts_File);                               //将一包数据写入文件
    pMuxItem->WritePacketNum ++;                                                      //已经写入文件的包个数++

	while(ts_pes->Pes_Packet_Length_Beyond)
	{
		ts_pos = 0;
		memset(TSbuf,0,TS_PACKET_SIZE); 

//		if ((WritePacketNum % 40) == 0)                                                          //每40个包打一个 pat,一个pmt
//		{
//			Write_Pat(m_One_Frame_Buf);                                                         //创建PAT
//			Write_Pmt(m_One_Frame_Buf);                                                         //创建PMT
//		}
		if(ts_pes->Pes_Packet_Length_Beyond >= 184)
		{
			//处理中间包
			WriteStruct_Packetheader(TSbuf,Video_Audio_PID,0x00,0x01, pMuxItem);     //PID = TS_H264_PID,不是有效荷载单元起始指示符_play_init = 0x00, ada_field_C,0x01,仅有有效负载；
			ts_pos += 4;
            memcpy(TSbuf + ts_pos,NeafBuf,184); 
			NeafBuf += 184;
			ts_pes->Pes_Packet_Length_Beyond -= 184;
			pOutput->OnTsPacket(TSbuf, TS_PACKET_SIZE, 0);
//			fwrite(TSbuf,188,1,pVideo_Audio_Ts_File);
		}
		else
		{
			if(ts_pes->Pes_Packet_Length_Beyond == 183||ts_pes->Pes_Packet_Length_Beyond == 182)
			{
				if ((pMuxItem->WritePacketNum % 40) == 0)                                                         //每40个包打一个 pat,一个pmt
				{
					Write_Pat(pMuxItem->m_One_Frame_Buf, pOutput);        //创建PAT
					Write_Pmt(pMuxItem->m_One_Frame_Buf, pOutput);        //创建PMT
				}

				WriteStruct_Packetheader(TSbuf,Video_Audio_PID,0x00,0x03, pMuxItem);   //PID = TS_H264_PID,不是有效荷载单元起始指示符_play_init = 0x00, ada_field_C,0x03,含有调整字段和有效负载；
				ts_pos += 4;
				TSbuf[ts_pos + 0] = 0x01;
				TSbuf[ts_pos + 1] = 0x00;
				ts_pos += 2;
				memcpy(TSbuf + ts_pos,NeafBuf,182); 
				  
				NeafBuf += 182;
				ts_pes->Pes_Packet_Length_Beyond -= 182;
				pOutput->OnTsPacket(TSbuf, TS_PACKET_SIZE, 0);
			}
			else
			{
				if ((pMuxItem->WritePacketNum % 40) == 0)                                                         //每40个包打一个 pat,一个pmt
				{
					Write_Pat(pMuxItem->m_One_Frame_Buf, pOutput);        //创建PAT
					Write_Pmt(pMuxItem->m_One_Frame_Buf, pOutput);        //创建PMT
				}

				WriteStruct_Packetheader(TSbuf,Video_Audio_PID,0x00,0x03, pMuxItem); //PID = TS_H264_PID,不是有效荷载单元起始指示符_play_init = 0x00, ada_field_C,0x03,含有调整字段和有效负载；
				ts_pos += 4;
				TSbuf[ts_pos + 0] = 184-ts_pes->Pes_Packet_Length_Beyond-1 ;
				TSbuf[ts_pos + 1] = 0x00;
				ts_pos += 2;
				memset(TSbuf + ts_pos,0xFF,(184 - ts_pes->Pes_Packet_Length_Beyond - 2)); 
				ts_pos += (184-ts_pes->Pes_Packet_Length_Beyond-2);
				memcpy(TSbuf + ts_pos,NeafBuf,ts_pes->Pes_Packet_Length_Beyond);
				ts_pes->Pes_Packet_Length_Beyond = 0;
				pOutput->OnTsPacket(TSbuf, TS_PACKET_SIZE, 0);
                pMuxItem->WritePacketNum ++;
			}
		}	
        pMuxItem->WritePacketNum ++;
	}

	return pMuxItem->WritePacketNum;
}

/*文件数据吸入ts*/
int WriteBuf2TsFile(unsigned int framerate, EStreamType eStreamType, unsigned char *pData, int iDataSize,
                    unsigned long long lTimeStamp,int type, IMuxOutput *pOutput)
{
    MuxItem *pMuxItem = pOutput->GetMuxItem();

    // 每个帧前均加上 PAT/PMT
	//if (type == 7)
    {
		//sps insert pat pmt
        pMuxItem->isInsertPatPmt = 0;
	}
	unsigned int   videoframetype = 0;    //视频帧类型
	Ts_Adaptation_field  ts_adaptation_field_Head;
	Ts_Adaptation_field  ts_adaptation_field_Tail;

    pMuxItem->Timestamp_audio = lTimeStamp%0xffffffff;
    pMuxItem->Timestamp_video = lTimeStamp%0xffffffff;
    /*
    // 通过帧率计算视频帧的时间戮增量（音频需要重新计算）
    if (eStreamType == 1 && (type == 5 || type == 1))
    {
        int nDeltaTime = 90000 / framerate;
        pMuxItem->Timestamp_video += nDeltaTime;
        pMuxItem->Timestamp_audio += nDeltaTime;
    }
	*/
	if (ESTREAM_AUDIO == eStreamType)
	{
        // 音频
		Take_Out_Pes(&pMuxItem->m_audio_tspes, pMuxItem->Timestamp_audio, 0x01, NULL, pData, iDataSize);
		if (pMuxItem->m_audio_tspes.Pes_Packet_Length_Beyond != 0)
		{
			//LOGE("PES_AUDIO  :  SIZE = %d\n", m_audio_tspes.Pes_Packet_Length_Beyond);
			//填写自适应段标志
			WriteAdaptive_flags_Tail(&ts_adaptation_field_Head); //填写自适应段标志  ,这里注意 音频类型不要算pcr 所以都用帧尾代替就行
			WriteAdaptive_flags_Tail(&ts_adaptation_field_Tail); //填写自适应段标志帧尾

			PES2TS(&pMuxItem->m_audio_tspes, TS_AAC_PID, &ts_adaptation_field_Head, &ts_adaptation_field_Tail,
                    pMuxItem->Timestamp_video, pMuxItem->Timestamp_audio, pOutput);//音频使用视频的时间戳
//			Timestamp_audio += 1024 * 1000 * 90 / audiosamplerate;
			//LOGE("pts_audio:%d ",Timestamp_audio);
           // pMuxItem->Timestamp_audio = lTimeStamp*90;
		}
	}
	else if (ESTREAM_VIDEO == eStreamType)
	{
        // 视频
		Take_Out_Pes(&pMuxItem->m_video_tspes, pMuxItem->Timestamp_video, 0x00, &videoframetype, pData, iDataSize, pMuxItem->uVideoEncType);
		if (pMuxItem->m_video_tspes.Pes_Packet_Length_Beyond != 0)
		{
		   // LOGE("PES_VIDEO  :  SIZE = %d\n", m_video_tspes.Pes_Packet_Length_Beyond);
			if (videoframetype == FRAME_I || videoframetype == FRAME_P || videoframetype == FRAME_B)
			{
                //pMuxItem->Timestamp_video = lTimeStamp*90;
				//填写自适应段标志
				WriteAdaptive_flags_Head(&ts_adaptation_field_Head, pMuxItem->Timestamp_video); //填写自适应段标志帧头
				WriteAdaptive_flags_Tail(&ts_adaptation_field_Tail); //填写自适应段标志帧尾
				//计算一帧视频所用时间
				PES2TS(&pMuxItem->m_video_tspes, TS_H264_PID, &ts_adaptation_field_Head, &ts_adaptation_field_Tail,
                        pMuxItem->Timestamp_video, pMuxItem->Timestamp_audio, pOutput);
//				pMuxItem->Timestamp_video += (90000 / framerate);
//				pMuxItem->Timestamp_video = pMuxItem->Timestamp_video%0xffffffff;

			}
			else
			{
				//填写自适应段标志
				WriteAdaptive_flags_Tail(&ts_adaptation_field_Head); //填写自适应段标志  ,这里注意 其它帧类型不要算pcr 所以都用帧尾代替就行
				WriteAdaptive_flags_Tail(&ts_adaptation_field_Tail); //填写自适应段标志帧尾
				PES2TS(&pMuxItem->m_video_tspes, TS_H264_PID, &ts_adaptation_field_Head, &ts_adaptation_field_Tail,
                        pMuxItem->Timestamp_video, pMuxItem->Timestamp_audio, pOutput);
			}
		}
	}
	return 1;
}